<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.forgetType" placeholder="类型" clearable style="width: 150px;" class="filter-item">
        <el-option v-for="item in typeList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.progress" placeholder="进度" clearable style="width: 150px;" class="filter-item">
        <el-option v-for="item in progressList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      :default-sort="{prop: 'forgetId', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed prop="forgetId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.forgetId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="forgetType" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" :type="typeMap[row.forgetType].type"> {{ typeMap[row.forgetType].label }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系手机号" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="progress" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="dark" :type="progressMap[row.progress].type"> {{ progressMap[row.progress].label }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发起时间" align="center" prop="createTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="handleTime" sortable="custom">
        <template slot-scope="{row}">
          <span v-if="row.handleTime">{{ row.handleTime }}</span>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button :disabled="row.progress!==0" type="success" size="mini" @click="handleUserForget(row.forgetId)">
            设为已处理
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.forgetId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showUserForgetList" />

  </div>
</template>

<script>
import { showUserForgetList, deleteUserForget, handleUserForget } from '@/api/users'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'UserForget',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        forgetType: undefined,
        progress: undefined
      },
      typeList: [
        { key: 1, value: '忘记密码' },
        { key: 2, value: '忘记手机号' }
      ],
      typeMap: {
        1: { type: 'primary', label: '忘记密码' },
        2: { type: 'success', label: '忘记手机号' }
      },
      progressList: [
        { key: 0, value: '待处理' },
        { key: 1, value: '已处理' }
      ],
      progressMap: {
        0: { type: 'primary', label: '待处理' },
        1: { type: 'success', label: '已处理' }
      }
    }
  },
  created() {
    this.showUserForgetList()
  },
  methods: {
    showUserForgetList() {
      this.listLoading = true
      showUserForgetList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showUserForgetList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    handleUserForget(forgetId) {
      this.$confirm('请确认该申请是否已处理完毕？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        handleUserForget(forgetId).then(() => {
          this.showUserForgetList()
          this.$notify({
            title: 'Success',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {})
    },
    handleDelete(forgetId) {
      this.$confirm('此删除操作不可逆，请确认是否要删除该申请?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserForget(forgetId).then(() => {
          this.showUserForgetList()
          this.$notify({
            title: 'Success',
            message: '删除找回账号申请成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>

<style>
  .col-title {
    text-align: right
  }

  .col-title p {
    font-weight: bolder;
  }

  .col-content p {
    color: #848383
  }
</style>
